/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.net.*;
import java.io.*;
import java.util.*;
import nanoxml.XMLElement;

/**
 *
 * @author marduke
 */
public class ClienteNodo {

    Socket requestSocket;
    ObjectOutputStream out;
    ObjectInputStream in;
    String message;
    String messageToServer;
    int puerto = 1800;
    String host;
    Vector vecinos = new Vector();

    public ClienteNodo() {
    }

    public ClienteNodo(String mensaje, String host) {
        this.host = host;
        this.messageToServer = mensaje;
        this.vecinos = vecinos;
    }

    String run() {
        String retorno = new String();
        try {
            //1. creating a socket to connect to the server
            
            requestSocket = new Socket(host, 1800);
            System.out.println("Connected to server");
            //2. get Input and Output streams
            out = new ObjectOutputStream(requestSocket.getOutputStream());
            out.flush();
            in = new ObjectInputStream(requestSocket.getInputStream());
            //3: Communicating with the server
            do {
                try {

                    message = (String) in.readObject();


                    System.out.println("serverNodo>" + message);
                    sendMessage(messageToServer);

                    

                    //Deberia recibir si es alcanze el xml pasado a string de los ip alcanzables.

                    message = (String) in.readObject();
                    if (message.compareTo("Error") == 0) {
                        System.out.println("Se produjo un error.");
                    } else {
                        retorno = message;
                    }
                    
                    System.out.println("serverNodo>" + message);

                    message = "bye";

                    sendMessage(message);



                } catch (ClassNotFoundException classNot) {
                    System.err.println("data received in unknown format");
                    
                }
            } while (!message.equals("bye"));
        } catch (UnknownHostException unknownHost) {
            System.err.println("El servidor no esta activado, o el ip no es el correcto.!");
//        } catch (ConnectException connectException) {
//            System.err.println("Estas tratando de conectarte a un puerto malo!");
//            System.exit(-2);
//        }
        } catch (ConnectException conE)  {
                    System.err.println("No existe ese nodo, el cual esta especificado en el archivo de maquinas");
                    return "Error";

                } 
        //         catch (SocketException socketEx) {
        //            System.out.println("Se perdio la conexion con el servidor, Vamos a intentar otra vez.");
        //            this.run();
        //        }
        catch (IOException ioException) {
            ioException.printStackTrace();
        } finally {
            //4: Closing connection
            try {
                in.close();
                out.close();
                requestSocket.close();
                ;
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

        return retorno;
    }



    void sendMessage(String msg) {
        try {
            out.writeObject(msg);
            out.flush();
            System.out.println("client>" + msg);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }
}
